package com.boarsoft.boar.batch.demo.biz;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.boar.batch.demo.entity.Coredpfm02;
import com.boarsoft.dao.sharding.bean.DaoSharding;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component("coredpfm02Biz")
public class Coredpfm02BizImpl extends SimpleBizImpl implements Coredpfm02Biz {
	public static final String INSERT_SQL = "insert into coredpfm02 (id, DM02ACCT, DM02ACCS, DM02ACCTYP, DM02HDFLG, DM02CLSFLG, DM02CCY, DM02CSHTRF, DM02PRDID, DM02SBNO, DM02OPNDAT, DM02STRDAT, DM02ENDDAT, DM02TERM1, DM02TERM2, DM02BALE, DM02BALD, DM02BALEN, DM02EBALD, DM02LBALE, DM02LBALD, DM02CTLBAL, DM02FRZBAL, DM02OVDBAL, DM02CVDB1, DM02CVDB2, DM02CVDB3, DM02ITPLAN, DM02ITFLT, DM02LSTDTE, DM02NODE, DM02ACCCHR, DM02ACCCH1, DM02YZFLG, DM02TDUCTL, DM02CSHFLG, DM02ADVTIM, DM02DMOD, DM02INTRAT, DM02FRZFLG, DM02MRTFLG, DM02PRVFLG, DM02PBLIN, DM02CVNFLG, DM02SECFLG, DM02OVDFLG, DM02IINFLG, DM02INTFLG, DM02INTFLG1, DM02NRAMT, DM02PMSAMT, DM02INTPRD, DM02DONTIM, DM02NXDAT, DM02LZFLG, DM02LIEID, DM02DTFLG, DM02ZCTIM, DM02CONNO, DM02FEEFLG, DM02REMARK, DM02OLBAL, DM02FLAG, DEL_F, LAST_ETL_ACG_DT) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

	@Autowired
	@Qualifier("dataSource")
	@Lazy
	private DataSource datasource;

	@Override
	@DaoSharding(policy = "user", index = 0)
	@Transactional
	public void save(int tenantId, Coredpfm02 dpfm) {
		dao.save(dpfm);
	}

	@Override
	@DaoSharding(policy = "user", index = 0)
	// @Transactional
	public void batchSave(int tenantId, List<Coredpfm02> lt) throws SQLException {
		// dao.batchSave(lt, 50);
		Connection conn = null;
		try {
			conn = datasource.getConnection();
			PreparedStatement ps = conn.prepareStatement(INSERT_SQL);
			conn.setAutoCommit(false);
			for (int i = 0; i < lt.size(); i++) {
				Coredpfm02 o = lt.get(i);
				ps.setString(1, o.getId());
				ps.setString(2, o.getDM02ACCT());
				ps.setString(3, o.getDM02ACCS());
				ps.setString(4, o.getDM02ACCTYP());
				ps.setString(5, o.getDM02HDFLG());
				ps.setString(6, o.getDM02CLSFLG());
				ps.setString(7, o.getDM02CCY());
				ps.setString(8, o.getDM02CSHTRF());
				ps.setString(9, o.getDM02PRDID());
				ps.setString(10, o.getDM02SBNO());
				ps.setString(11, o.getDM02OPNDAT());
				ps.setString(12, o.getDM02STRDAT());
				ps.setString(13, o.getDM02ENDDAT());
				ps.setBigDecimal(14, o.getDM02TERM1());
				ps.setString(15, o.getDM02TERM2());
				ps.setBigDecimal(16, o.getDM02BALE());
				ps.setString(17, o.getDM02BALD());
				ps.setBigDecimal(18, o.getDM02BALEN());
				ps.setString(19, o.getDM02EBALD());
				ps.setBigDecimal(20, o.getDM02LBALE());
				ps.setString(21, o.getDM02LBALD());
				ps.setBigDecimal(22, o.getDM02CTLBAL());
				ps.setBigDecimal(23, o.getDM02FRZBAL());
				ps.setBigDecimal(24, o.getDM02OVDBAL());
				ps.setBigDecimal(25, o.getDM02CVDB1());
				ps.setBigDecimal(26, o.getDM02CVDB2());
				ps.setBigDecimal(27, o.getDM02CVDB3());
				ps.setString(28, o.getDM02ITPLAN());
				ps.setBigDecimal(29, o.getDM02ITFLT());
				ps.setString(30, o.getDM02LSTDTE());
				ps.setString(31, o.getDM02NODE());
				ps.setString(32, o.getDM02ACCCHR());
				ps.setString(33, o.getDM02ACCCH1());
				ps.setString(34, o.getDM02YZFLG());
				ps.setString(35, o.getDM02TDUCTL());
				ps.setString(36, o.getDM02CSHFLG());
				ps.setBigDecimal(37, o.getDM02ADVTIM());
				ps.setString(38, o.getDM02DMOD());
				ps.setBigDecimal(39, o.getDM02INTRAT());
				ps.setString(40, o.getDM02FRZFLG());
				ps.setString(41, o.getDM02MRTFLG());
				ps.setString(42, o.getDM02PRVFLG());
				ps.setBigDecimal(43, o.getDM02PBLIN());
				ps.setString(44, o.getDM02CVNFLG());
				ps.setString(45, o.getDM02SECFLG());
				ps.setString(46, o.getDM02OVDFLG());
				ps.setLong(47, o.getDM02IINFLG());
				ps.setLong(48, o.getDM02INTFLG());
				ps.setLong(49, o.getDM02INTFLG1());
				ps.setBigDecimal(50, o.getDM02NRAMT());
				ps.setBigDecimal(51, o.getDM02PMSAMT());
				ps.setBigDecimal(52, o.getDM02INTPRD());
				ps.setBigDecimal(53, o.getDM02DONTIM());
				ps.setString(54, o.getDM02NXDAT());
				ps.setString(55, o.getDM02LZFLG());
				ps.setBigDecimal(56, o.getDM02LIEID());
				ps.setString(57, o.getDM02DTFLG());
				ps.setBigDecimal(58, o.getDM02ZCTIM());
				ps.setString(59, o.getDM02CONNO());
				ps.setString(60, o.getDM02FEEFLG());
				ps.setString(61, o.getDM02REMARK());
				ps.setBigDecimal(62, o.getDM02OLBAL());
				ps.setString(63, o.getDM02FLAG());
				ps.setLong(64, o.getDEL_F());
				ps.setString(65, o.getLAST_ETL_ACG_DT());
				ps.addBatch();
				if (i % 100 == 0) {
					ps.executeBatch();
					conn.commit();
				}
			}
			ps.executeBatch();
			conn.commit();
		} finally {
			if (conn != null) {
				conn.close();
			}
		}
	}

	@Override
	@DaoSharding(policy = "user", index = 0)
	@Transactional(readOnly = true)
	public List<Coredpfm02> list(int tenantId, String hql, int pageNo, int pageSize) {
		return dao.list(hql, pageNo, pageSize);
	}
}
